import os
import sys

class PathResolver:
    @staticmethod
    def is_frozen():
        """检查是否在PyInstaller打包环境中运行"""
        return getattr(sys, 'frozen', False)
    
    @staticmethod
    def get_base_path():
        """获取基础路径"""
        if PathResolver.is_frozen():
            # PyInstaller创建的临时文件夹
            base_path = sys._MEIPASS
        else:
            # 开发环境，项目根目录
            base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        return base_path
    
    @staticmethod
    def resolve(relative_path):
        """解析相对路径为绝对路径，处理打包环境中的路径差异"""
        base_path = PathResolver.get_base_path()
        
        # 特殊处理日志目录
        if "log_dir" in relative_path:
            return os.path.join(base_path, 'logs')
        
        # 处理模型路径
        if "model_path" in relative_path:
            return os.path.join(base_path, 'assets', 'model', 'yolo.pt')
        
        # 处理其他资源
        return os.path.join(base_path, relative_path.lstrip('../').lstrip('../../'))